package com.ainibaby6.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.collections.MapUtils;

import com.ainibaby6.domain.models.DictInfo;

public class DictUtils
{
    // private static volatile long updateTime = System.currentTimeMillis();

    private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    private static Map<String, Map<String, DictInfo>> dictMap = new HashMap<String, Map<String, DictInfo>>();

    public static String getDictName(String type, String key)
    {
        lock.readLock().lock();
        try
        {
            checkInit();
            Map<String, DictInfo> map = dictMap.get(type);
            if (map == null)
            {
                return null;
            }
            DictInfo info = map.get(key);
            return info == null ? null : info.getDictValue();
        }
        finally
        {
            lock.readLock().unlock();
        }
    }

    public static List<DictInfo> getTypeDicts(String type)
    {
        lock.readLock().lock();
        try
        {
            checkInit();
            Map<String, DictInfo> map = dictMap.get(type);
            if (map == null)
            {
                return null;
            }
            List<DictInfo> dicts = new ArrayList<DictInfo>();
            for (DictInfo dictInfo : map.values())
            {
                dicts.add(dictInfo);
            }
            return dicts;
        }
        finally
        {
            lock.readLock().unlock();
        }
    }

    public static String addDictInfo(DictInfo info)
    {
        lock.writeLock().lock();
        try
        {
            String id = UUIDGenerator.generate();
            info.setDictId(id);
            info.save();
            initDictInfo(info);
            return id;
        }
        finally
        {
            lock.writeLock().unlock();
        }
    }

    private static void checkInit()
    {
        lock.readLock().unlock();
        lock.writeLock().lock();
        try
        {
            if (MapUtils.isEmpty(dictMap))
            {
                List<DictInfo> list = DictInfo.find(
                        "from DictInfo order by dictType Asc,dictValue asc")
                        .fetch();
                for (DictInfo dictInfo : list)
                {
                    initDictInfo(dictInfo);
                }
            }
        }
        finally
        {
            lock.readLock().lock();
            lock.writeLock().unlock();
        }
    }

    private static void initDictInfo(DictInfo info)
    {
        Map<String, DictInfo> map = dictMap.get(info.getDictType());
        if (map == null)
        {
            map = new TreeMap<String, DictInfo>();
            dictMap.put(info.getDictType(), map);
        }
        map.put(info.getDictKey(), info);
    }
}
